let userDao = require("../dao/userDao");
let managerDao = require("../dao/managerDao");
let UserPojo = require("../models/UserPojo")
let ManagerPojo = require("../models/ManagerPojo")
let JsonMessage = require("../models/JsonMessage")

let checkIdAndPassword = function(id,password) {

}
module.exports = {
    userRegister: function (req, res) {
        let userModel = new ManagerPojo();
        userModel.id = req.body.id;
        userModel.password = req.body.password
        userModel.fammily_name = req.body.fammilyName
        userModel.address = req.body.address
        userModel.email = req.body.email
        userModel.signup_time = Math.round(new Date().getTime() / 1000)
        userModel.given_name = req.body.givenName
        userDao.insertUser(userModel, req, res);
    },
    managerLogout: function (req, res) {
        if(!req.session.userid) {
            res.json(new JsonMessage("4004",null,"You(manager) hadn't log in"))
        }
        else {
            console.log("即将退出的id"+req.session.userid)
            req.session.destroy()
            res.json(new JsonMessage("2001",null,"log out Success" ))
        }
    },
    userLogout: function (req, res) {
        if(!req.session.userid) {
            res.json(new JsonMessage("4003",null," You(user) hadn't log in"))
        }
        else {
            req.session.destroy()
            res.json(new JsonMessage("2001",null,"log out Success" ))
        }
    },
    managerRegister: function (req, res) {
        let userPojo = new UserPojo();
        userPojo.id = req.body.id;
        userPojo.password = req.body.password
        userPojo.fammily_name = req.body.fammilyName
        userPojo.address = req.body.address
        userPojo.email = req.body.email
        userPojo.signup_time = Math.round(new Date().getTime() / 1000)
        userPojo.given_name = req.body.givenName
        userDao.insertUser(userPojo, req, res);
    },
    userLogin: function (req, res) {
        let id = req.body.id
        let userPassword = req.body.password

        userDao.selectUserNameAndPassword(id).then((results) => {

            if (results.length>0) {
                if (userPassword==results[0].password) {
                    req.session.userid=id
                    let username = results[0].fammily_name+results[0].given_name
                    req.session.username=username
                    res.json({status:"1101",data: {"id":id,"username":username},message:"success"})
                }else{
                    res.json({status:"4101",data:null,message:"User ID or password incorrect"})
                }
            }
            else {
                res.json({status:"4104",data:null,message:"no such a user"})
            }


        })

    },
    managerLogin: function (req,res) {

        let id = req.body.id
        let managerInputPassword = req.body.password

        managerDao.selectManager(id).then((results) => {
            if (results.length>0) {
                if (managerInputPassword==results[0].password) {
                    req.session.userid=id
                    let username = results[0].fammily_name+results[0].given_name
                    req.session.username=username
                    res.json({status:"1001",data:{"id":id,"username":username},message:"manager password correct"})
                }else{
                    res.json({status:"4001",data:null,message:"manager password incorrect"})
                }
            }
            else {
                res.json({status:"4002",data:null,message:"no such a manager"})
            }


        })
    }


}